<?php

class LoginAction extends Action {

    
    public function index(){
        
        $this->display();
    }
    
    public function registPage(){
        $this->display();
    }
    
    public function runRegist(){
        //echo 'ok';
        $db_members = M('Members');
        
        if($db_members->create()){
            $db_members->password = I("password","","md5");
            $db_members->create_time = date("Y-m-d H:i:s",time());
            
            $res = $db_members->where(array("username"=>$db_members->username))->find();
            if(!$res){
                if($db_members->add()){
                    $this->ajaxReturn(array('status' => 1, 'info' => '注册成功！待管理员审核...', 'data' => null, 'jumpUrl' => U('index') ));
                }else{
                    $this->ajaxReturn(array('status' => 0, 'info' => '注册失败...', 'data' => null, 'jumpUrl' => U('index') ));
                }
            }else{
                $this->ajaxReturn(array('status' => -1, 'info' => '该编号已被注册过...', 'data' => null, 'jumpUrl' => U('index') ));
            }
        }

    }
    
    public function runLogin(){
        $username = I('username', '');
        $password = I('password', '');
        
        $res = D('Members')->checkLogin($username, $password);

        switch ($res) {
            case -1:
                $this->ajaxReturn(array('status' => -1, 'info' => '用户不存在', 'data' => null, 'jumpUrl' => null));
                break;
            case 0:
                $this->ajaxReturn(array('status' => 0, 'info' => '密码错误', 'data' => null, 'jumpUrl' => null));
                break;
            case 1:
                $this->ajaxReturn(array('status' => 1, 'info' => '登陆成功', 'data' => null, 'jumpUrl' => U('Index/Index/index')));
                break;
            case 2:
                $this->ajaxReturn(array('status' => 2, 'info' => '管理员还未处理...', 'data' => null, 'jumpUrl' => null));
                break;
        }
        

    }
    
    
    public function forgetPwd(){
        
        $username = I('username', '');
        
        $db_members = M('Members');
        
        $res = $db_members->where(array("username"=>$username))->find();
        
        if($res){
            
            if($res['status'] == 1){
                $db_members->where(array("username"=>$username))->setField('status',2);
                $this->ajaxReturn(array('status' => 1, 'info' => '已通知管理员！待管理员处理...', 'data' => null, 'jumpUrl' => U('index') ));
            }elseif($res['status'] == 0){
                $this->ajaxReturn(array('status' => -1, 'info' => '新注册学员！待管理员处理...', 'data' => null, 'jumpUrl' => null ));
            }elseif($res['status'] == 2){
                $this->ajaxReturn(array('status' => 2, 'info' => '已提交申请...', 'data' => null, 'jumpUrl' => null ));
            }
        }else{
            $this->ajaxReturn(array('status' => 0, 'info' => '用户名不存在！', 'data' => null, 'jumpUrl' => null ));
        }

    }

    public function login_bak(){

    	$uid = I("get.id", "-1");
    	$password = I("get.p", "");
    	
        //if( D('Members', 'Service')->checkLogin($uid, $password) ) {
    	if( D('Members')->checkLogin($uid, $password) ) {
    		$this->redirect("Index/index");
    	}else{
    		$this->error('没有权限');
    	}

    }
}